package com.product.android.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.product.android.business.headImage.HeadImageLoader.ImageType;
import com.product.android.commonInterface.contact.RankInfo;
import com.product.android.ui.widget.TopRankItemView;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class TopRankListViewAdapter extends BaseAdapter{
	
	private Context mContext;
	private List<RankInfo> mlistRank;
	private int mTopType;
	private int mScreenWidth;
	
	public TopRankListViewAdapter(Context context, int topType, int screenWidth){
		mContext = context;
		mlistRank = new ArrayList<RankInfo>();
		mTopType = topType;
		mScreenWidth = screenWidth;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(mlistRank == null)return 0;
		int size = mlistRank.size();
		switch(size){
		case 0:
		case 1:
			return size;
		case 2:
		case 3:
			return 2;
			default:
				return (size - 1) / 3 + 2;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mlistRank.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			LinearLayout llItem = new LinearLayout(mContext);
			TopRankItemView firstView = new TopRankItemView(mContext);
			TopRankItemView secondtView = new TopRankItemView(mContext);
			TopRankItemView thirdView = new TopRankItemView(mContext);
			LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			layout.weight = 1.0f;
			layout.setMargins(3,3,3,3);
			llItem.addView(firstView, layout);
			llItem.addView(secondtView, layout);
			llItem.addView(thirdView, layout);
			convertView = llItem;
		}
		
		LinearLayout llItem = (LinearLayout) convertView;
		TopRankItemView firstView = (TopRankItemView) llItem.getChildAt(0);
		TopRankItemView secondtView = (TopRankItemView) llItem.getChildAt(1);
		TopRankItemView thirdView = (TopRankItemView) llItem.getChildAt(2);
		switch(position){
		//第一行
		case 0:
			firstView.setData(mlistRank.get(0), mScreenWidth, mTopType, ImageType.LARGE);
			firstView.setVisibility(View.VISIBLE);
			secondtView.setVisibility(View.GONE);
			thirdView.setVisibility(View.GONE);
			break;
		//第二行
		case 1:
			firstView.setData(mlistRank.get(1), mScreenWidth, mTopType, ImageType.MIDIUM);
			firstView.setVisibility(View.VISIBLE);
			
			if(mlistRank.size() > 2){
				secondtView.setData(mlistRank.get(2), mScreenWidth, mTopType, ImageType.MIDIUM);
				secondtView.setVisibility(View.VISIBLE);
			}else{
				secondtView.setVisibility(View.INVISIBLE);
			}
			
			thirdView.setVisibility(View.GONE);
			break;
			//第三行以后
			default:
				int beginindex = 3 * position - 3;
				firstView.setData(mlistRank.get(beginindex), mScreenWidth, mTopType, ImageType.DEFAULT);
				firstView.setVisibility(View.VISIBLE);
				
				if(mlistRank.size() > beginindex + 1){
					secondtView.setData(mlistRank.get(beginindex + 1), mScreenWidth, mTopType, ImageType.DEFAULT);
					secondtView.setVisibility(View.VISIBLE);
				}else{
					//这边要用INVISIBLE，否则会显示成第二行那样
					secondtView.setVisibility(View.INVISIBLE);
				}
				
				if(mlistRank.size() > beginindex + 2){
					thirdView.setData(mlistRank.get(beginindex + 2), mScreenWidth, mTopType, ImageType.DEFAULT);
					thirdView.setVisibility(View.VISIBLE);
				}else{
					thirdView.setVisibility(View.INVISIBLE);
				}
				
				break;
		}
		
		return convertView;
	}
	
	public void setRankInfos(List<RankInfo> listRank){
		mlistRank = listRank;
	}
}
